<template>
  <!-- 整个容器 -->
  <el-container class="home-container">
    <!--头部区域  -->
    <el-header>
      <div class="header-left">
        <img src="../assets/heima.png" />
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!--侧边导航栏 -->
      <el-aside :width="isCollapse ? '64px' :'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单区域  unique-opened表示是否只保持一个子菜单的展开
        将二级菜单的index值赋值给default-active属性，激活菜单的高亮效果
        -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单,通过v-for来渲染一级菜单 -->
          <!-- 因为为每一个循环添加的index值一样，所以菜单的行为保持了一致，改变index的值，对其进行动态绑定 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 菜单模板 -->
            <template slot="title">
              <!-- 菜单图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 菜单文字 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item 
            :index="'/'+subItem.path "
            v-for="subItem in item.children"
            :key="subItem.id"
            @click="saveNavState('/'+subItem.path)"
            >
              <template slot="title">
                <!-- 菜单图标 -->
                <i class="el-icon-menu"></i>
                <!-- 菜单文字 -->
                <span>{{subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域,展示区渲染组件，放一个路由占位符 -->
      <el-main><router-view></router-view></el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "home",
  data() {
    return {
      menuList: [],
      iconsObj:{
          '125':"el-icon-user-solid",
          '103':"el-icon-monitor",
          '101':"el-icon-s-goods",
          '102':"el-icon-s-order",
          '145':"el-icon-data-line",
      },
      isCollapse:false,
      activePath:""
    };
  },
  created() {
    this.getMenuList();
    // 刷新创建时，在会话存储中立马把激活路径拿出来，赋值给左侧菜单进行高亮
    this.activePath=window.sessionStorage.getItem("activePath")
  },
  methods: {
    loginOut() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    toggleCollapse(){
        this.isCollapse=!this.isCollapse
    },
    saveNavState(activePath){
        window.sessionStorage.setItem("activePath",activePath)
        // 在点击的时候拿到激活路径进行高亮效果
        this.activePath=activePath
    }
  },
};
</script>

<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
}
.header-left {
  display: flex;
  align-items: center;
}
span {
  margin-left: 15px;
}
.el-aside {
  background-color: #333744;
}
.el-menu{
    border-right: none;
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button{
    background-color: #4A5064;
    line-height: 24px;
    font-size:10px;
    color: #fff;
    text-align: center;
    /* 表示三个线之间的距离 */
    letter-spacing: 0.2em;
    cursor:pointer

}
</style>